#pragma once
#include "CommPort.h"
#include "CommunicationDevice.h"

class CCommunicationSerialPort : public Jin::CSharedObject
{
public:
	typedef Jin::CSharedObjectPtrT<CCommunicationSerialPort>	SharedPtr;

public:
	CCommunicationSerialPort(void);
	virtual ~CCommunicationSerialPort(void);

	BOOL Open(LPCTSTR);
	void Close(void);
	BOOL SetTB8(BOOL);

	const CString& GetPortName(void) const
	{
		return m_strCommPort;
	}

	BOOL ReadData(LPBYTE, DWORD, DWORD&, HANDLE);
	BOOL WriteData(LPBYTE, DWORD, DWORD&, HANDLE);

	CCommunicationDevice* AddCommunicationDevice(BYTE, COMMDEVICECALLBACK, LPVOID);
	void RemoveCommunicationDevice(CCommunicationDevice*);

private:
	static UINT WINAPI _RTXDThread(LPVOID);
	UINT _RTXD(void);

private:
	CString					m_strCommPort;
	HANDLE					m_hComm;
	volatile BOOL			m_bRun;

	std::list<CCommunicationDevice::SharedPtr>		m_vCommDevices;
	Jin::CCriticalSection							m_csCommDevicesLock;

	HANDLE					m_hRTXThread;
	HANDLE					m_hRTXEvent;
};

